package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.CommonAccessLog;
import com.millstein.tsinglog.annotation.ResponseResult;
import com.millstein.tsinglog.commons.constant.SystemConstant;
import com.millstein.tsinglog.model.dto.comment.CommentSaveDTO;
import com.millstein.tsinglog.model.vo.biz.CommentVo;
import com.millstein.tsinglog.model.vo.result.PageDetail;
import com.millstein.tsinglog.service.ArticleService;
import com.millstein.tsinglog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 局部页面刷新对应的控制器
 */
@Controller
@RequestMapping("/portal")
public class AjaxPortalController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    /**
     * 为article.html准备评论数据
     * @param model
     * @param aid
     * @param currentPage
     * @return
     */
    @RequestMapping("/getCommentForPortal")
    public String getCommentForPortal(Model model, Long aid,
                                      @RequestParam(value = "page", required = false) Integer currentPage) {
        // 给页面准备该文章的评论
        PageDetail<CommentVo> commentPage = commentService.getCommentByAidForPortal(aid, currentPage);

        model.addAttribute("commentPage", commentPage);

        return "article::list_refresh";
    }

    /**
     * 为guestBook.html准备留言数据
     * @param model
     * @param currentPage
     * @return
     */
    @RequestMapping("/getGuestBookForPortal")
    public String getGuestBookForPortal(Model model,
                                        @RequestParam(value = "page", required = false) Integer currentPage) {
        // 给留言板准备留言数据，即aid为0的所有评论
        PageDetail<CommentVo> commentPage = commentService.getCommentByAidForPortal(0L, currentPage);

        model.addAttribute("commentPage", commentPage);

        return "guestBook::list_refresh";
    }

    @RequestMapping("/supportComment")
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("支持评论")
    public int supportComment(Long cid, Long aid) {
        return commentService.doWithComment(cid, aid, SystemConstant.TYPE_FOR_DO_SUPPORT_FOR_COMMENT);
    }

    @RequestMapping("/opposeComment")
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("反对评论")
    public int opposeComment(Long cid, Long aid) {
        return commentService.doWithComment(cid, aid, SystemConstant.TYPE_FOR_DO_OPPOSE_FOR_COMMENT);
    }

    @RequestMapping("/preferThisArticle")
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("文章点赞")
    public int preferThisArticle(Long aid) {
        return articleService.preferThisArticle(aid);
    }

    @RequestMapping(value = "/submitComment", method = RequestMethod.POST)
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("发表评论")
    public void submitComment(@Valid @RequestBody CommentSaveDTO commentSaveDTO) {
        commentService.saveComment(commentSaveDTO);
    }

    @RequestMapping("/supportGuestBook")
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("支持留言")
    public int supportGuestBook(Long cid) {
        return commentService.doWithComment(cid, 0L, SystemConstant.TYPE_FOR_DO_SUPPORT_FOR_COMMENT);
    }

    @RequestMapping("/opposeGuestBook")
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("反对留言")
    public int opposeGuestBook(Long cid) {
        return commentService.doWithComment(cid, 0L, SystemConstant.TYPE_FOR_DO_OPPOSE_FOR_COMMENT);
    }

    @RequestMapping(value = "/submitGuestBook", method = RequestMethod.POST)
    @ResponseBody
    @ResponseResult
    @CommonAccessLog("发表留言")
    public void submitGuestBook(@Valid @RequestBody CommentSaveDTO commentSaveDTO) {
        commentService.saveComment(commentSaveDTO);
    }
}
